package com.sayno.wrench.dynamic.config.center.domain.service;

import com.sayno.wrench.dynamic.config.center.config.DynamicConfigCenterAutoConfig;
import com.sayno.wrench.dynamic.config.center.config.DynamicConfigCenterAutoConfigProperties;
import com.sayno.wrench.dynamic.config.center.domain.model.valobj.AttributeVo;
import com.sayno.wrench.dynamic.config.center.types.annotations.DCCValue;
import jodd.util.StringUtil;
import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.framework.AopProxy;
import org.springframework.aop.framework.AopProxyUtils;
import org.springframework.aop.support.AopUtils;

import java.lang.reflect.Field;

import static com.sayno.wrench.dynamic.config.center.types.common.Constants.SYMBOL_COLON;

/**
 * @description:
 * @author: Administrator
 * @createTime: 2025/5/13 22:39
 */
public class DynamicConfigCenterService implements IDynamicConfigCenterService{


    private final Logger log = LoggerFactory.getLogger(DynamicConfigCenterService.class);

    private final DynamicConfigCenterAutoConfigProperties dynamicConfigCenterAutoConfigProperties;

    private final RedissonClient  redissonClient;

    public DynamicConfigCenterService(DynamicConfigCenterAutoConfigProperties dynamicConfigCenterAutoConfigProperties, RedissonClient redissonClient) {
        this.dynamicConfigCenterAutoConfigProperties = dynamicConfigCenterAutoConfigProperties;
        this.redissonClient = redissonClient;
    }


    @Override
    public Object proxyObject(Object bean) {
        Class<?> beanClass = bean.getClass();

        Object targetBean = bean;
        if (AopUtils.isAopProxy(bean)){
            beanClass = AopUtils.getTargetClass(bean);
            targetBean = AopProxyUtils.getSingletonTarget(bean);
        }

        Field[] fields = beanClass.getDeclaredFields();
        for (Field field : fields) {

            if (!field.isAnnotationPresent(DCCValue.class)) continue;

            DCCValue annotation = field.getAnnotation(DCCValue.class);
            String value = annotation.value();
            if (StringUtil.isBlank(value)){
                throw new RuntimeException("err");
            }

            String[] split = value.split(SYMBOL_COLON);

        }
        return null;
    }

    @Override
    public void adjustAttributeValue(AttributeVo attributeVo) {

    }
}
